package com.gao.api.gate.quick.tt;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.pool.ChannelHealthChecker;
import io.netty.channel.pool.ChannelPoolHandler;
import io.netty.channel.pool.FixedChannelPool;

import java.time.Duration;

/**
 * <p></p>
 *
 * @author gaoshuangwen
 * @since 2022/02/14 21:13
 */
public  class IdleChannelPool extends FixedChannelPool{
    private long created;
    private long updated;
    private Duration maxChannelPoolIdleTime;
    private Duration maxChannelPoolLifeTime;
    public boolean isExpired(){
        if (maxChannelPoolLifeTime != null) {
            if (created + maxChannelPoolLifeTime.toMillis() <= System.currentTimeMillis()) {
                return true;
            }
        }
        if(maxChannelPoolIdleTime == null){
            return false;
        }
        long ttl = updated - created;

        return maxChannelPoolIdleTime.toMinutes() <= ttl;
    }
    public void update(){
        this.updated = System.currentTimeMillis();
    }
    public void create(){
        this.created = System.currentTimeMillis();
    }
    public IdleChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler, int maxConnections) {
        this(bootstrap, handler, maxConnections, Integer.MAX_VALUE);
    }

    public IdleChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler, int maxConnections, int maxPendingAcquires) {
        super(bootstrap, handler, maxConnections, maxPendingAcquires);
        this.create();
        this.update();
    }

    public IdleChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler, ChannelHealthChecker healthCheck, AcquireTimeoutAction action, long acquireTimeoutMillis, int maxConnections, int maxPendingAcquires) {
        super(bootstrap, handler, healthCheck, action, acquireTimeoutMillis, maxConnections, maxPendingAcquires);
    }

    public IdleChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler, ChannelHealthChecker healthCheck, AcquireTimeoutAction action, long acquireTimeoutMillis, int maxConnections, int maxPendingAcquires, boolean releaseHealthCheck) {
        super(bootstrap, handler, healthCheck, action, acquireTimeoutMillis, maxConnections, maxPendingAcquires, releaseHealthCheck);
    }

    public IdleChannelPool(Bootstrap bootstrap, ChannelPoolHandler handler, ChannelHealthChecker healthCheck, AcquireTimeoutAction action, long acquireTimeoutMillis, int maxConnections, int maxPendingAcquires, boolean releaseHealthCheck, boolean lastRecentUsed) {
        super(bootstrap, handler, healthCheck, action, acquireTimeoutMillis, maxConnections, maxPendingAcquires, releaseHealthCheck, lastRecentUsed);
    }


}
